/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cs425proj.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author prateekpatil
 */
public class RequestForward extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
   
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException
            {
            response.setContentType("text/html");
            PrintWriter out= response.getWriter();

            String username= request.getParameter("username");
            String psw= request.getParameter("password");

            String className= "oracle.jdbc.driver.OracleDriver";
            String url= "jdbc:oracle:thin:@fourier.cs.iit.edu:1521:orcl";
            String user= "rjairaj";
            String password= "cs425dbo";

            Connection con;
            PreparedStatement ps;
            ResultSet rs;

            try
            {
            Class.forName(className);
            con= DriverManager.getConnection(url, user, password);

            String sql= "SELECT username FROM member WHERE username = '"+username+"' AND password = '"+psw+"'";
            ps= con.prepareStatement(sql);
            rs= ps.executeQuery();
            if(rs.next())
            {
            HttpSession session= request.getSession();
            String n= rs.getString("username");
            session.setAttribute("name", n);
            

            ServletContext context= getServletContext();
          //  response.sendRedirect("/index.jsp");
            RequestDispatcher rd= context.getRequestDispatcher("/ForwardServlet");
            rd.forward(request, response);
            }
            else
            {
            ServletContext context= getServletContext();
            RequestDispatcher rd= context.getRequestDispatcher("/index.jsp");
            out.println("<font color=red>invalid user name or password</font>");
            rd.include(request, response);
            }

            con.close();
            ps.close();
            rs.close(); 
            }
            catch(ClassNotFoundException cx)
            {
            out.println();
            }
            catch(SQLException sx)
            {
            out.println();
            }
            }
  
       
}
